/*
  cpgf Library
  Copyright (C) 2011 Wang Qi http://www.kbasm.com/
  All rights reserved.

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
*/


#ifndef __GMETAVARIANT_H
#define __GMETAVARIANT_H

#include "cpgf/gvariant.h"
#include "cpgf/gmetatype.h"
#include "cpgf/gclassutil.h"

namespace cpgf {

#pragma pack(push, 1)
#pragma pack(1)
struct GMetaVarData
{
	GVariantData varData;
	GMetaTypeData typeData;
};
#pragma pack(pop)


GMAKE_FINAL(GMetaVariant)

class GMetaVariant : GFINAL_BASE(GMetaVariant)
{
public:
	GMetaVariant() {
	}

	template <typename T>
	GMetaVariant(const T & v) : value(v), type(createMetaType<T>()) {
		fixupMetaType(&this->type);
	}

	GMetaVariant(const GMetaVarData & data) : value(data.varData), type(data.typeData) {
	}

	GMetaVariant(const GVariant & v, const GMetaType & type) : value(v), type(type) {
	}

	GMetaVariant(const GMetaVariant & other) : value(other.value), type(other.type) {
	}

	GMetaVariant & operator = (const GMetaVariant & other) {
		this->value = other.value;
		this->type = other.type;

		return *this;
	}

	const GVariant & getValue() const {
		return this->value;
	}

	const GMetaType & getType() const {
		return this->type;
	}

	GMetaVarData getData() const {
		GMetaVarData data;

		data.varData = this->value.getData();
		data.typeData = this->type.getData();

		return data;
	}

	GMetaVarData takeData() {
		GMetaVarData data;

		data.varData = this->value.takeData();
		data.typeData = this->type.getData();

		return data;
	}

private:
	GMetaVariant(const GVariant &);

private:
	GVariant value;
	GMetaType type;
};



} // namespace cpgf


#endif
